/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
const app = getApp()
import __config from '../../../config/env'

Page({
    data: {
        config: app.globalData.config,
        wxUser: null,
        userInfo: null,
        mode: 'date',
        isLoading: false,
        isFreezing: false,
    },
    onShow() {
        app.initPage()
            .then(res => {
                let wxUser = app.globalData.wxUser
                this.setData({
                    wxUser: wxUser,
                    isLoading: false,
                    isFreezing: wxUser.status == '1' ? true : false
                })
                this.wxUserGet()
            })
    },
    onLoad() {
    },
    /**
     * 小程序设置
     */
    settings: function () {
        wx.openSetting({
            success: function (res) {
                console.log(res.authSetting)
            }
        })
    },
    getUserProfile(e) {
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (detail) => {
                app.api.wxUserSave(detail)
                    .then(res => {
                        let wxUser = res.data
                        this.setData({
                            wxUser: wxUser
                        })
                        app.globalData.wxUser = wxUser
                        this.wxUserGet()
                    })
            }
        })
    },
    //获取商城用户信息
    wxUserGet() {
        app.api.wxUserGet()
            .then(res => {
                this.setData({
                    userInfo: res.data
                })
            })
    },
    onPickerChange(e) { //返回回调函数
        console.log("onPickerChange", e)
        app.globalData.wxUser.birthday = e.detail.value;
        this.setData({
            wxUser: app.globalData.wxUser
        })
        app.api.wxUserPut(app.globalData.wxUser).then(res => {
            console.log(res)
            if (res.data) {
                wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
    },
    onAddressChange() {
        let that = this
        wx.showModal({
            editable: true,
            placeholderText: '请输入您的地址',
            showCancel: true,
            title: '修改地址',
            success(res) {
                if (res.confirm) {
                    console.log(res)
                    app.globalData.wxUser.address = res.content;
                    that.setData({
                        wxUser: app.globalData.wxUser
                    })
                    app.api.wxUserPut(app.globalData.wxUser).then(res => {
                        console.log(res)
                        if (res.data) {
                            wx.showToast({
                                title: '成功',
                                icon: 'success',
                                duration: 2000
                            })
                        }
                    })
                } else if (res.cancel) {
                    console.log('cancel')
                }
            }
        })
    },
    getPhoneNumber(e) {
        console.log(e.detail.code)
        var that = this;
        let errMsg = e.detail.errMsg;
        if (errMsg == 'getPhoneNumber:ok') {
            return new Promise((resolve, reject) => {
                wx.login({
                    success: function (res) {
                        if (res.code) {
                            app.api.login({
                                jsCode: res.code
                            })
                                .then(res => {
                                    let wxUser = res.data
                                    app.globalData.wxSessionKey = wxUser.wxSessionKey
                                    app.globalData.wxUser = wxUser
                                    wx.checkSession({
                                        success: function () {
                                            console.log(e.detail.errMsg)
                                            console.log(e.detail.iv)
                                            console.log(e.detail.encryptedData)
                                            var ency = e.detail.encryptedData;
                                            var iv = e.detail.iv;
                                            var sessionk = app.globalData.wxSessionKey;
                                            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
                                                that.setData({
                                                    modalstatus: true
                                                });

                                            } else { //同意授权
                                                wx.request({
                                                    method: "GET",
                                                    url: __config.basePath + '/app/api/deciphering',
                                                    data: {
                                                        encrypdata: ency,
                                                        ivdata: iv,
                                                        sessionkey: sessionk
                                                    },
                                                    header: {
                                                        'content-type': 'application/json' // 默认值
                                                    },
                                                    success: (res) => {
                                                        console.log(res);
                                                        if (res.data != "") {
                                                            console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");
                                                            var phone = res.data.phoneNumber;
                                                            console.log(phone);
                                                            app.globalData.wxUser.phone = phone
                                                            that.setData({
                                                                wxUser: app.globalData.wxUser
                                                            })
                                                            app.api.wxUserPut(app.globalData.wxUser).then(res => {
                                                                console.log(res)
                                                                if (res.data) {
                                                                    wx.showToast({
                                                                        title: '成功',
                                                                        icon: 'success',
                                                                        duration: 2000
                                                                    })
                                                                }
                                                            });
                                                        } else {
                                                            wx.showToast({
                                                                title: '获取失败',
                                                                icon: 'error',
                                                                duration: 2000
                                                            })
                                                        }
                                                    },
                                                    fail: function (res) {
                                                        console.log("解密失败~~~~~~~~~~~~~");
                                                        console.log(res);
                                                    }
                                                });
                                            }
                                        },
                                        fail: function () {
                                            console.log("session_key 已经失效，需要重新执行登录流程");
                                            that.wxlogin(); //重新登录
                                        }
                                    });
                                    resolve("success")
                                })
                        }
                    },
                    fail: function (res) {
                        console.log(res)
                    }
                })
            })
        } else if (errMsg = "fail no permission") {
            wx.showToast({
                title: '没有权限',
                icon: 'error',
                duration: 2000
            })
        } else {
            wx.showToast({
                title: '获取失败',
                icon: 'error',
                duration: 2000
            })
        }
    },
    oneKeyReport() {
        app.api.oneKeyRepair({}).then(res => {
            wx.showToast({
                title: '上报成功',
            })
        })
    },
    reportAdmin() {
        wx.makePhoneCall({
            phoneNumber: '13312345678',
        })
    }
})